package com.saohuobang.test.listener;

import ch.qos.logback.classic.LoggerContext;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * 作者 yaohua.liu
 * 日期 2015-11-20 19:19
 * 包名 com.saohuobang.test.listener
 * 说明
 * Avoiding memory leaks

 If your application is deployed in a web-server or an application server, the registration of an JMXConfigurator instance creates a reference from the system class loader into your application which will prevent it from being garbage collected when it is stopped or re-deployed, resulting in a severe memory leak.

 Thus, unless your application is a standalone Java application, you MUST unregister the JMXConfigurator instance from the JVM's Mbeans server. Invoking the reset() method of the appropriate LoggerContext will automatically unregister any JMXConfigurator instance. A good place to reset the logger context is in the contextDestroyed() method of a javax.servlet.ServletContextListener.
 */

public class JMXConfiguratorListener implements ServletContextListener {

    public void contextDestroyed(ServletContextEvent sce) {
        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
        lc.stop();
    }

    public void contextInitialized(ServletContextEvent sce) {
    }
}